package com.qdlc.p2p.manage.action.additional;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.CardSendService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.model.CardSendModel;

/**
 * Created by lenovo on 2015/12/21.
 */
public class CardSendAction extends BaseAction<CardSendModel> implements ModelDriven<CardSendModel> {

    @Resource
    private CardSendService cardSendService;

    private Map<String, Object> map = new HashMap<String, Object>();

    /**
     * 列表页面
     *
     * @return
     * @throws Exception
     */
    @Action("/modules/additional/cardSend/manager")
    public String cardSendManager() throws Exception {
        return "manager";
    }

    /**
     * 列表
     *
     * @throws Exception
     */
    @Action("/modules/additional/cardSend/cardSendList")
    public void cardSendList() throws Exception {
        PageDataList<CardSendModel> pageList = cardSendService.findByModel(model);
        int totalPage = pageList.getPage().getTotal(); // 总页数
        map.put("total", totalPage);
        map.put("rows", pageList.getList());
        printJson(getStringOfJpaObj(map));
    }

    /**
     * 导出Excel_获取列表信息
     * @throws Exception
     */
    @Action("/modules/additional/cardSend/exportExcelList")
    public void exportExcelList() throws Exception{
        model.setPage(1);
        model.setRows(5000);
        // 初始化status值为99
        PageDataList<CardSendModel> pageDataList = cardSendService.findByModel(model);

        String title = "寄卡信息Excel表";

        String[] hearders = new String[] { "编号", "寄卡人姓名", "寄卡人电话", "寄卡人邮箱", "寄卡人地址", "收卡人姓名", "收卡人电话","收卡人邮箱", "收卡人地址", "备注","添加时间" };// 表头数组
        String[] fields = new String[] { "id", "sendName", "sendPhone","sendEmail", "sendAddress", "receiveName", "receivePhone","receiveEmail", "receiveAddress","remark","addTime"};// 对象属性数组
        TableData td = ExcelUtils.createTableData(pageDataList.getList(), ExcelUtils.createTableHeader(hearders), fields);
        JsGridReportBase report = new JsGridReportBase(request, response);
        report.exportToExcel(title, getOperator().getUserName(), td);
    }
}
